#!/bin/bash

echo "=== 税务数据报送系统启动脚本 ==="

# 检查Java环境
if ! command -v java &> /dev/null; then
    echo "错误: 未找到Java环境，请先安装JDK 8+"
    exit 1
fi

echo "Java版本: $(java -version 2>&1 | head -n 1)"

# 检查Maven环境
if ! command -v mvn &> /dev/null; then
    echo "错误: 未找到Maven环境，请先安装Maven 3.6+"
    exit 1
fi

echo "Maven版本: $(mvn -version 2>&1 | head -n 1)"

# 检查MySQL服务
echo "检查MySQL服务..."
if ! systemctl is-active --quiet mysql && ! systemctl is-active --quiet mysqld; then
    echo "警告: MySQL服务未启动，请确保数据库服务正常运行"
fi

# 检查Redis服务
echo "检查Redis服务..."
if ! systemctl is-active --quiet redis && ! systemctl is-active --quiet redis-server; then
    echo "警告: Redis服务未启动，请确保Redis服务正常运行"
fi

echo ""
echo "=== 启动后端服务 ==="
echo "正在启动Spring Boot应用..."

# 启动后端服务
cd "$(dirname "$0")"
mvn spring-boot:run &

# 等待后端服务启动
echo "等待后端服务启动..."
sleep 30

# 检查后端服务状态
if curl -s http://localhost:8080/api > /dev/null; then
    echo "✅ 后端服务启动成功！"
    echo "后端API地址: http://localhost:8080/api"
    echo "数据库监控: http://localhost:8080/api/druid"
else
    echo "❌ 后端服务启动失败，请检查日志"
    exit 1
fi

echo ""
echo "=== 启动前端服务 ==="
echo "正在启动Vue3前端..."

# 启动前端服务
cd frontend
npm install
npm run dev &

# 等待前端服务启动
echo "等待前端服务启动..."
sleep 10

echo "✅ 前端服务启动成功！"
echo "前端地址: http://localhost:3000"

echo ""
echo "=== 系统启动完成 ==="
echo "请访问 http://localhost:3000 使用系统"
echo ""
echo "按 Ctrl+C 停止所有服务"

# 等待用户中断
wait
